<?php

/**
 * Description of G_Memcache
 *
 * @author Monk <eoe2005@gmail.com>
 */
if(class_exists('Memcache') === false){
    trigger_error('系统不支持Memcache模块');
    die();
}
class G_Memcache extends GC_Cache
{
    protected $_memcache;
    public function __construct($servers)
    {
        $this->_memcache = new Memcache;
        foreach($servers AS $val){
            $this->_memcache->addserver($val['host'], $val['port'], false, $val['weight']);
        }
    }
    public function dec($key, $step = 1)
    {
        return $this->_memcache->decrement($key,$step);
    }

    public function delete($keys)
    {
        if(is_array($keys)){
            foreach($keys AS $k){
                $this->_memcache->delete($k);
            }
            return true;
        }else{
            return $this->_memcache->delete($keys);
        }
    }

    public function exists($keys)
    {
        if(is_array($keys)){
            $rs = array();
            foreach($keys AS $k){
                if($this->_memcache->get($k)){
                    $rs[] = $k;
                }
            }
            return $rs;
        }else{
            return $this->_memcache->get($key) ? true : false;
        }
    }

    public function flushAll()
    {
        return $this->_memcache->flush();
    }

    public function get($key)
    {
        $r = $this->_memcache->get($key);
        if($r){
            $r = unserialize($r);
        }
        return $r;
    }

    public function inc($key, $step = 1)
    {
        return $this->_memcache->increment($key,$step);
    }

    public function set($key, $val, $timeout = 0)
    {
        return $this->_memcache->set($key,serialize($val),MEMCACHE_COMPRESSED,$timeout);
    }

}
